package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.Roles;
import org.example.models.R;
import org.example.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/roles")
public class RolesController {

    @Autowired
    RolesService rolesService;

    @GetMapping("/page")
    public R<Page<Roles>> page(
            @RequestParam(name="current",defaultValue = "1") int current,
            @RequestParam(name="size",defaultValue = "2") int size,
            @RequestParam(name="name",required = false) String name
    ){
        QueryWrapper<Roles> queryWrapper = new QueryWrapper<>();//查询条件
        if(name != null && name.length() > 0){
            queryWrapper.lambda().like(Roles::getName,name);
        }
        Page<Roles> page = new Page<>(current, size);
        rolesService.page(page,queryWrapper);
        return R.success(page);
    }

    @PostMapping
    public R insert(@RequestBody Roles roles){
        rolesService.save(roles);
        return R.success();
    }

    @PutMapping
    public R update(@RequestBody Roles roles){
        rolesService.updateById(roles);
        return R.success();
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") String id){
        rolesService.removeById(id);
        return R.success();
    }

}
